package com.cb.customviewdrawing;

import java.util.Stack;

public class chenbing {

    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<Integer>();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        System.out.println(queue.empty());
        queue.push(3);
        queue.push(4);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.empty());
    }

    static class MyQueue<T> {
        private Stack<T> pushStack;
        private Stack<T> popStack;

        public MyQueue() {
            pushStack = new Stack<>();
            popStack = new Stack<>();
        }

        /**
         * Push element x to the back of queue.
         */
        public void push(T x) {
            while (!popStack.isEmpty()) {
                pushStack.push(popStack.pop());
            }
            pushStack.push(x);
        }

        /**
         * Removes the element from in front of queue and returns that element.
         */
        public T pop() {
            if (pushStack.isEmpty()) {
                return popStack.pop();
            }
            while (!pushStack.isEmpty()) {
                popStack.push(pushStack.pop());
            }
            return popStack.pop();
        }

        /**
         * Get the front element.
         */
        public T peek() {
            if (pushStack.isEmpty()) {
                return popStack.peek();
            }
            while (!pushStack.isEmpty()) {
                popStack.push(pushStack.pop());
            }
            return popStack.peek();
        }

        /**
         * Returns whether the queue is empty.
         */
        public boolean empty() {
            return pushStack.isEmpty() && popStack.isEmpty();
        }
    }
}
